package com.longjianghuasheng.logistics.weight.custom

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import com.blankj.utilcode.util.VibrateUtils
import com.google.android.material.checkbox.MaterialCheckBox
import com.longjianghuasheng.logistics.util.CacheUtil
import com.longjianghuasheng.logistics.R
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.navigateAction

class CollectView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) :
    MaterialCheckBox(context, attrs, defStyleAttr) {

    private var onCollectViewClickListener: OnCollectViewClickListener? = null


    init {


//        setOnTouchListener(this)
    }


    @SuppressLint("MissingPermission")
    fun onTouch(v: View?, event: MotionEvent?): Boolean {

        when (event?.action) {
            MotionEvent.ACTION_UP -> {
                //震动一下
                VibrateUtils.vibrate(40)
                if (!CacheUtil.isLogin) {
                    onCollectViewClickListener?.onClick(this)
                } else {
                    isChecked = true
                    nav(v!!).navigateAction(R.id.loginFragment)
                }

            }
        }
        return false


    }

    fun setOnCollectViewClickListener(onCollectViewClickListener: OnCollectViewClickListener) {
        this.onCollectViewClickListener = onCollectViewClickListener
    }

    interface OnCollectViewClickListener {
        fun onClick(v: CollectView)
    }
}